//
//  jiaViewController.m
//  模拟-百思不得姐
//
//  Created by 谭浔明 on 16/8/5.
//  Copyright © 2016年 chunyu. All rights reserved.
//

#import "jiaViewController.h"

#import "GetFilePath.h"
#import "DBSphereView.h"
#define wh = [UIScreen mainScreen].bounds.size.width

@interface jiaViewController ()<UIImagePickerControllerDelegate,UINavigationControllerDelegate>
@property (weak, nonatomic) IBOutlet UITextView *textVi;

@property (weak, nonatomic) IBOutlet UIButton *fatuping;

@property (weak, nonatomic) IBOutlet UIImageView *biaotupian;

@property (weak, nonatomic) IBOutlet UILabel *fa2;

@property (weak, nonatomic) IBOutlet UIView *vie;

@property (nonatomic, retain) DBSphereView *sphereView;

@property (nonatomic ,strong) NSTimer *timer1;
@property (nonatomic ,strong) NSTimer *timer2;

@end

@implementation jiaViewController

@synthesize sphereView;




- (void)viewDidLoad {
    [super viewDidLoad];
  _textVi.editable = NO;
    
    
    
    [self add];
    sphereView = [[DBSphereView alloc] initWithFrame:CGRectMake(100, 420, 100, 100)];
    NSMutableArray *array = [[NSMutableArray alloc] initWithCapacity:0];
    for (NSInteger i = 0; i < 50; i ++) {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
        [btn setTitle:[NSString stringWithFormat:@"%ld", i] forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
        btn.titleLabel.font = [UIFont systemFontOfSize:24.];
        btn.frame = CGRectMake(0, 0, 60, 20);
        [btn addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
        [array addObject:btn];
        [sphereView addSubview:btn];
    }
    [sphereView setCloudTags:array];
    sphereView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:sphereView];
    

}

- (void)buttonPressed:(UIButton *)btn
{
    [sphereView timerStop];
    
    [UIView animateWithDuration:0.3 animations:^{
        btn.transform = CGAffineTransformMakeScale(2., 2.);
    } completion:^(BOOL finished) {
        [UIView animateWithDuration:0.3 animations:^{
            btn.transform = CGAffineTransformMakeScale(1., 1.);
        } completion:^(BOOL finished) {
            [sphereView timerStart];
        }];
    }];
}


 


-(void)add{


    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        self.timer1 = [NSTimer scheduledTimerWithTimeInterval:0.5f target:self selector:@selector(timerAction) userInfo:nil repeats:YES];
        self.timer2 = [NSTimer scheduledTimerWithTimeInterval:0.8f target:self selector:@selector(timerAction) userInfo:nil repeats:YES];
    });
    
}



- (void)timerAction
{
    UIImageView *imgV = [[UIImageView alloc] initWithFrame:CGRectMake([self arcNumber], -25, 25, 25)];
    imgV.image = [UIImage imageNamed:@"Unknown.png"];
    [self.view addSubview:imgV];
    [UIView animateWithDuration:8 animations:^{
        imgV.frame = CGRectMake([self arcNumber], self.view.frame.size.height + 25, 25, 25);
    } completion:^(BOOL finished) {
        if (finished) {
            [imgV removeFromSuperview];
        }
    }];
}

- (CGFloat)arcNumber
{
    return arc4random() % (int)(self.view.frame.size.width - 25);
}
  

- (void)viewWillAppear:(BOOL)animated
{

    
     
    
    
    [UIView animateWithDuration:1 animations:^{
       
        
        _fatuping.y +=self.view.width;
        _fa2.y += self.view.width;
 
        _textVi.y -=self.view.width;
 
        
        _biaotupian.y -=self.view.width;
         
        
    } completion:^(BOOL finished) {
        
    }];
    
    
    [self.view addSubview:_biaotupian];
    _fa2.alpha = 0.1;
    // 1、开始动画
    [UIView beginAnimations:nil context:nil];
    // 2、动画执行时间
    [UIView setAnimationDuration:1];
    // 3、动画执行次数
    [UIView setAnimationRepeatCount:FLT_MAX];
    // 4、最终效果
    _fa2.alpha = 1;
    // 5、开始动画
    [UIView commitAnimations];
    
    
}
//取消按钮
- (IBAction)cancel:(UIButton *)sender {
  
    [UIView animateWithDuration:1 animations:^{
    
        
        _fatuping.y +=self.view.width;
        _fa2.y += self.view.width;
        
        _textVi.y +=self.view.width;

        
        _biaotupian.y -=self.view.width;
        
    } completion:^(BOOL finished) {
        
    }];
    
    //延迟时间
    int64_t delayInSeconds = 0.9999999999999999999999999;
 
    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
    dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
    printf("回到主页");
        UIView *v=[[UIView alloc]init];
        v.backgroundColor=[UIColor whiteColor];
        
        [self dismissViewControllerAnimated:YES completion:nil];
        
    
    });
}





- (IBAction)PictureBtn:(UIButton *)sender {
    CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"position.x"];
    CGFloat centerX = self.fatuping.center.x;
    NSNumber *l = [NSNumber numberWithFloat:centerX - 10];
    NSNumber *c = [NSNumber numberWithFloat:centerX];
    NSNumber *r = [NSNumber numberWithFloat:centerX + 10];
    animation.values = @[l, c, r, c, l, c, r, c, l, c];
    animation.duration = 0.5;
    animation.repeatCount = 100;
    [self.fatuping.layer addAnimation:animation forKey:nil];
    
    
}



- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
 
        // 获取视频url路径
        NSURL *url = [info objectForKey:UIImagePickerControllerMediaURL];
        NSString *path = [url path];
        // 视频保存
        BOOL  b = UIVideoAtPathIsCompatibleWithSavedPhotosAlbum(path);
        if (b) {
            UISaveVideoAtPathToSavedPhotosAlbum(path,nil,nil,nil);
          
            
        }
  
}




//进入拍摄页面点击取消按钮
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    [self dismissViewControllerAnimated:YES completion:nil];
}






- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
